package com.technology.education.ui.student.course_detail

import androidx.lifecycle.MutableLiveData
import com.technology.education.data.model.CourseDetailBean
import com.technology.education.data.model.CourseDetailModel
import com.technology.education.data.model.CourseVideo
import com.technology.education.ext.toast
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

/**
 *@author:create by BZY
 *Date: 2022/3/11 11
 */
class CourseDetailViewModel : BaseViewModel() {
    val hasCollected = BooleanObservableField(false)
    val result=MutableLiveData<ResultState<CourseVideo>>()
    val detail=MutableLiveData<ResultState<CourseDetailBean>>()
    var id = ""
    var url=""
    var title=""

    /**
     * 当前正在播放的课程id
     */
    var currentId=""

    override fun getData() {
        super.getData()
        request({ apiService.courseVideoDetail(id)},result)
    }

    fun getDetail(){
        request({ apiService.courseDetail(id)},detail)
    }

    fun toggleCollected() {
        request({
            apiService.collectCourseVideo(
                hashMapOf(
                    "id" to id,
                    "state" to if (hasCollected.get()) 0 else 1
                )
            )
        }, success = {
            hasCollected.set(!hasCollected.get())
        }, error = {
            toast(it.errorMsg)
        })

    }
}